package com.geek.javase.ch17.work.uploadwork;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/*
 *@Date 2025/11/4
 *@Author wangpeng
 *@Description TODO
 */
public class UploadClient {
    public static void main(String[] args) {
        try(
                //将文件从磁盘读入内存中
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\CodeFile\\igeeklearn\\testJavaSE\\src\\com\\geek\\javase\\ch17\\work\\test.png"));
                //创建客户端
                Socket socket = new Socket(InetAddress.getLocalHost(), 8899);
                //通过Socket写消息，写至服务器
                BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());

                //读取服务器端回传的上传成功消息
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        ){
            //文件上传 边读边写
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len=bis.read(bytes)) != -1){
                bos.write(bytes, 0, len);
            }
            //写完刷新
            bos.flush();
            //写完socket停止输出
            socket.shutdownOutput();
            //读上传成功的消息
            System.out.println(br.readLine());
        }catch(FileNotFoundException e){
            e.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}
